/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.service;

import com.ivanbiz.dao.DaftarKelasDAO;
import com.ivanbiz.dao.GLAccountDAO;
import com.ivanbiz.dao.InvoiceDAO;
import com.ivanbiz.dao.PerusahaanDAO;
import com.ivanbiz.dao.impl.GLAccountDAOImpl;
import com.ivanbiz.dao.impl.PerusahaanDAOImpl;
import com.ivanbiz.model.Invoice;
import com.ivanbiz.model.Pembayaran;
import com.ivanbiz.model.Perusahaan;
import com.ivanbiz.report.GlobalReport;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JRParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import org.zkoss.util.media.AMedia;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Iframe;

/**
 *
 * @author IW20149X
 */
public class PembayaranTagihanLaporanViewService extends SelectorComposer<Component> {

    @Wire
    Iframe ipdf;
    PerusahaanDAO perusahaanDAO;
    NumberFormat numberFormat;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        numberFormat = NumberFormat.getNumberInstance();

        perusahaanDAO = new PerusahaanDAOImpl();
        List<Perusahaan> listPerusahaan = perusahaanDAO.getAll(Perusahaan.class);
        Perusahaan perusahaan = listPerusahaan.get(0);
        perusahaan.setAlamat(perusahaan.getAlamat() + "\n" + "Ph  :" + perusahaan.getTelephone() + "\n" + "Fax :" + perusahaan.getFax());

        Pembayaran pembayaran = (Pembayaran) Executions.getCurrent().getArg().get("pembayaran");

        Invoice invoice = pembayaran.getInvoice();
        invoice.setTerbilang("## " + ServiceHelper.bilang(Integer.parseInt(String.valueOf(new Double(pembayaran.getJumlah()).intValue()))) + " rupiah ##");
        pembayaran.setInvoice(invoice);

        GlobalReport globalReport = new GlobalReport();
        globalReport.setPerusahaan(perusahaan);
        globalReport.setPembayaran(pembayaran);
        globalReport.setLogo(Executions.getCurrent().getDesktop().getWebApp().getRealPath("/img/logo.jpg"));
        globalReport.setJumlah("Rp " + numberFormat.format(pembayaran.getJumlah()) + ",00");

        List<GlobalReport> listReport = new ArrayList<GlobalReport>();
        listReport.add(globalReport);

        String reportFile = Executions.getCurrent().getDesktop().getWebApp().getRealPath("/report/PembayaranReport.jasper");

        JRDataSource dataSource = new JRBeanCollectionDataSource(listReport);

        Map map = new HashMap();
        map.put(JRParameter.REPORT_DATA_SOURCE, dataSource);

        JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile, map);
        loadToStream(jasperPrint);

    }

    private void loadToStream(JasperPrint jasperPrint) throws JRException, IOException {
        ByteArrayOutputStream bytesOutputStream = new ByteArrayOutputStream();
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(bytesOutputStream);

        JRExporter exporter = new JRPdfExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, bufferedOutputStream);
        exporter.exportReport();

        InputStream mediais = new ByteArrayInputStream(bytesOutputStream.toByteArray());
        bytesOutputStream.close();

        AMedia amedia = new AMedia(jasperPrint.getName() + ".pdf", "pdf", "application/pdf", mediais);
        ipdf.setContent(amedia);
    }
}
